<?php
include "./index/control/IndexControl.php";
include "./index/libs/Tag.class.php";
include "./index/libs/functions.php";
class HtmlControl extends Control{
    public $staticAll=0;
	//首页静态
	function index(){
		ob_start();
		$control = new IndexControl;
		$control->index();
		$htmlData = ob_get_clean();
		if(file_put_contents("index.html", $htmlData)){
            if($this->staticAll==1){
                return true;
            }
            $this->success("生成静态首页成功！");
		}else{
			$this->error("首页生成失败！");
		}
	}
	//列表页静态
	function cat(){
		$db = M("category");
        $cat = $db->field("cathtml,cid,archtml,catdir")->all();
        $control = new IndexControl;
        //生成目录
        foreach ($cat as $c) {
            $caturl = get_cat_url($c, 2);
            $path = dirname($caturl);
            is_dir($path) || dir_create($path);
            $_GET['cid'] = $c['cid'];
            ob_start();
            $control->cat();
            $data = ob_get_clean();
            file_put_contents($path . '/index.html', $data);
        }
        if(is_dir($path)){
            $dirCont = scandir($path);
            if(count($dirCont)==3){
                if($this->staticAll==1){
                    return true;
                }
                $this->success("生成静态列表页成功！");    
            }else{
                 $this->error("生成静态列表页失败！");
            }
        }
	}
	//生成文章
    function arc($id = null) {
        $db = K("arc");
        $where = $id ? " aid =$id" : "";
        $arcs = $db->where($where)->field("aid,cathtml,arc.cid,archtml,catdir")->all();
        $control = new IndexControl;
        //生成目录
        $okFlag = TRUE;
        foreach ($arcs as $c) {
            $caturl = $this->getArcHtmPath($c);
            $path = dirname($caturl);
            is_dir($path) || dir_create($path);
            $_GET['aid'] = $c['aid'];
            ob_start();
            $control->arc();
            $data = ob_get_clean();
            if(FALSE == file_put_contents($caturl, $data)){
                $this->error("生成静态内容页失败！");
                $okFlag =FALSE;
            }
        }
        if($okFlag){
            if($this->staticAll==1){
                return true;
            }
            $this->success("生成静态内容页成功！");
        }
    }

    //一键全部静态
    function all(){
        $this->staticAll=1;
        if($this->index()){
           if($this->cat()){
                if($this->arc()){
                    $this->success("生成全站静态成功！");
                }
           }
        }
    }
    /**
     * 获得静态文件存放路径
     * @param type $field
     * @return type
     */
    private function getArcHtmPath($field) {
        $date = getdate();
        $search = array(
            '{htmlpath}',
            '{catdir}',
            '{aid}',
            '{y}',
            '{m}',
            '{d}'
        );
        $replace = array(
            'html',
            $field['catdir'],
            $field['aid'],
            $date['year'],
            $date['mon'],
            $date['mday']
        );
        $path = str_replace($search, $replace, $field['archtml']);
        return PATH_ROOT . '/' . $path;
    }
}

?>